package cn.microservices.cloud.common.core.utils;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 枚举工具类
 */
public class EnumsUtils {
    /**
     * 根据Code 获取Value值
     * @param code
     * @return
     */
    public static String getValueByCode(Class clz,String code){

        try {
            Object[] objects = clz.getEnumConstants();
            Method getCode  = clz.getMethod("getCode");
            Method getName = clz.getMethod("getValue");
            for (Object obj : objects){
                if (code.equals(getCode.invoke(obj))){
                    return String.valueOf(getName.invoke(obj));
                }
            }

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 根据Code 获取Value值
     * @param code
     * @return
     */
    public static String getTextByCode(Class clz,String code){

        try {
            Object[] objects = clz.getEnumConstants();
            Method getCode  = clz.getMethod("getCode");
            Method getName = clz.getMethod("getText");
            for (Object obj : objects){
                if (code.equals(getCode.invoke(obj))){
                    return String.valueOf(getName.invoke(obj));
                }
            }

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

}
